package net.fonline.apps;

import java.io.PrintStream;
import javax.microedition.lcdui.Graphics;

/**
 * The name is wrong. It is no entity, more likely a map-tile.
 * This object contains information about a field (is there a tower or not, ...)
 * @author Michael
 */
public class Entity {

    private EchoCanvas ec;
    private int x;
    private int y;
    private int width;
    private int height;
    private int ID;
    public static int maxID = 0;
    private Monster aim = null;
    public boolean active = false;
    private boolean path = false;
    private int reload = 0;
    private int recoil = 500;
    private int damage_type = 0;
    private int damage_power = 10;
    private int level = 0;
    private int range = 4000;
    private int beam_despawn = 200;

    public Entity(EchoCanvas ec, int x, int y, int width, int height, boolean round) {
        this.ec = ec;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.ID = (maxID + 1);
        ec.event_handler.append(new EventEntity(String.valueOf(this.ID), this.x, this.y, this.width, this.height));
        maxID += 1;
    }

    public void useForPath() {
        path = true;
    }

    public void resetPath() {
        path = false;
    }

    public void move(double time) {
        //Status ändern, wenn es angeklickt wird
        if (this.ec.event_handler.check(String.valueOf(this.ID))) {
            if (this.active) {
                if (level < 1) {
                    level++;
                } else {
                    if (damage_type < 1) {
                        damage_type = 1;
                        level = 0;
                        damage_power = 10;
                        recoil = 500;
                    } else {
                        this.active = false;
                    }
                }
            } else {
                this.active = true;
                damage_type = 0;
                damage_power = 1000;
                recoil = 2000;
                level = 0;
            }
        }
        // Move if there is no cheat, otherwise stop shooting
        if (!ec.cheatbot.cheat && this.active) {
            if (aim != null && aim.isDead()) {
                aim = null;
            }
            // if there is no aim find a new one
            if (aim == null) {
                aim = ec.map.monsters.getRoot();
                while (aim != null) {
                    if (pyt(aim.x - x, aim.y - y) < range) {
                        break;
                    }
                    aim = aim.next;
                }
            }
            if (aim != null && pyt(aim.x - x, aim.y - y) > range) {
                aim = null;
            }
            if (aim != null && reload < 0) {
                aim.damage(damage_type, damage_power+((damage_power*level)/2));
                ec.missiles.add(new Missile(x, y, (int) aim.x, (int) aim.y, damage_type, beam_despawn));
                reload = recoil;
            }
            reload -= time;
        }
    }

    public void update(Graphics g) {
        if (this.ec.popup) {
            if (!this.active);
        } else {
            g.setColor(50, 100, 50);
            g.fillRect((int) (this.x * this.ec.scale) + this.ec.relX, (int) (this.y * this.ec.scale) + this.ec.relY, (int) (this.width * this.ec.scale), (int) (this.height * this.ec.scale));
            if (this.active) {
                g.setColor(100, 80, 50);
                g.fillArc((int) ((this.x + 2) * this.ec.scale) + this.ec.relX, (int) ((this.y + 2) * this.ec.scale) + this.ec.relY, (int) ((this.width - 4) * this.ec.scale), (int) ((this.height - 4) * this.ec.scale), 0, 360);
                g.setColor(255, 255, 255);
                g.drawString(damage_type+" "+(level+1), (int) ((this.x + 2) * this.ec.scale) + this.ec.relX, (int) ((this.y + 2) * this.ec.scale) + this.ec.relY, 20);
            }
            if (this.path) {
                g.setColor(255, 255, 255);
                g.fillArc((int) ((this.x + 8) * this.ec.scale) + this.ec.relX, (int) ((this.y + 8) * this.ec.scale) + this.ec.relY, (int) ((this.width - 16) * this.ec.scale), (int) ((this.height - 16) * this.ec.scale), 0, 360);
            }
        }
    }

    private double pyt(double d, double d0) {
        return (d * d) + (d0 * d0);
    }
}